package com.wu.service.weixin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wu.common.constant.Constant;
import com.wu.common.page.PageResult;
import com.wu.common.service.impl.BaseServiceImpl;
import com.wu.common.utils.*;
import com.wu.dao.weixin.dao.WeixinMenuDao;
import com.wu.dao.weixin.vo.WeixinMenuVO;
import com.wu.entity.weixin.po.WeixinAccount;
import com.wu.entity.weixin.po.WeixinMenu;
import com.wu.service.sys.query.SysUserQuery;
import com.wu.service.weixin.service.WxMenuService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户管理
 *
 * @author wzd
 */
@Service
@AllArgsConstructor
public class WxMenuServiceImpl extends BaseServiceImpl<WeixinMenuDao, WeixinMenu> implements WxMenuService {
    private final RedisUtils redisUtils;

    @SneakyThrows
    @Override
    public PageResult<WeixinMenuVO> page(SysUserQuery query) {
        // 查询参数
        Map<String, Object> params = getParams(query);

        // 分页查询
        IPage<WeixinMenu> page = getPage(query);
        params.put(Constant.PAGE, page);
        // 数据列表
        List<WeixinMenu> list = baseMapper.getList(params);
        if(Objects.isNull(list)){
            return new PageResult<WeixinMenuVO>(new ArrayList<>(), page.getTotal());
        }else{
            Map<String, WeixinAccount> resultMap = (Map<String, WeixinAccount>) redisUtils.get(RedisKeys.getWxAccountMpKey(""));
            List<WeixinMenuVO> listVo = new ArrayList<>();
            Map<String, WeixinMenu> tag = list.stream().collect(Collectors.toMap(p->p.getId(),p->p));
            list.forEach(item->{
                WeixinMenuVO vo = new WeixinMenuVO();
                BeanUtils.copyProperties(item,vo);
                vo.setFatherName(StringUtils.isEmpty(item.getFatherId())?"-":tag.get(item.getFatherId()).getMenuName());
                vo.setJwName(resultMap.get(item.getJwid()).getAccountname());
                listVo.add(vo);
            });
            return new PageResult<WeixinMenuVO>(listVo, page.getTotal());
        }


    }

    @Override
    public void deleteByIds(List<String> idList) {
        baseMapper.deleteByIds(idList);
    }

    @Override
    public void save(WeixinMenuVO vo) {
        WeixinMenu entity = new WeixinMenu();
        BeanUtils.copyProperties(vo,entity);
        baseMapper.insert(entity);
    }

    @Override
    public void update(WeixinMenuVO vo) {
        WeixinMenu entity = new WeixinMenu();
        BeanUtils.copyProperties(vo,entity);
        baseMapper.update(entity);
    }

    @Override
    public WeixinMenuVO selectById(String id) {
        WeixinMenu entity = baseMapper.get(id);
        //获取父菜单信息
        WeixinMenu faentity = baseMapper.get(entity.getFatherId());
        WeixinMenuVO vo = new WeixinMenuVO();
        BeanUtils.copyProperties(entity,vo);
        Map<String, WeixinAccount> resultMap = (Map<String, WeixinAccount>) redisUtils.get(RedisKeys.getWxAccountMpKey(""));
        vo.setFatherName(faentity==null?"":faentity.getMenuName());
        vo.setJwName(resultMap.get(vo.getJwid()).getAccountname());
        return vo;
    }


    private Map<String, Object> getParams(SysUserQuery query){
        Map<String, Object> params = new HashMap<>();
        params.put("username", query.getUsername());
        params.put("mobile", query.getMobile());
        params.put("gender", query.getGender());

        // 数据权限
        params.put(Constant.DATA_SCOPE, getDataScope("t1", null));

        return params;
    }


}
